home *** CD-ROM | disk | FTP | other *** search
- Path: thor.tu.hac.com!collins
- From: collins@thor.tu.hac.com (Ron Collins)
- Newsgroups: comp.lang.c
- Subject: Re: Why doesn't this work?
- Date: 6 Mar 1996 00:12:33 GMT
- Organization: Advanced Depot Systems
- Message-ID: <4hil9h$hh5@hacgate2.hac.com>
- References: <1996Mar4.161412.137442@forest>
- NNTP-Posting-Host: thor.tu.hac.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- ebromber@forest.drew.edu wrote:
- : Does anyone know why this password program doesn't work properly? And if
- : you do know the problem how can I fix it? It rejects every password including
- : the real password. The program was compiled using a MS-DOS compiler.
- : Thanks in advance.
- : ebromber@drew.edu
-
- : main();
- : { char real[4];
- : char pass[100];
- : int count=0;
- : int i, error;
- : char c;
- : real[0]='j';real[1]='e';real[2]='r';real[3]='k';
- : printf("PASSWORD: ");
- : fflush(stdout);
- : while (c=getch() !='\n')
- : { count++;
- : pass[count]=c;
- : putch('*');
- : }
-
- Change the above to look something like:
-
- while ((c = getch()) != '\n')
- {
- pass[count++] = c;
- putch('*');
- };
-
- That is, increment "count" _after_ assigning the character "c" to "pass".
-
- : if (count!=4) { printf("\nWRONG PASSWORD\n"); main();}
- : error=0;
- : for (i=0; i<4; i++)
- : if (real[i]=pass[i]) error++;
- : if (error>0) {printf("\nWRONG PASSWORD\n"); main();}
- : }
-
-
- By incrementing "count" before the assignment, you started saving char "c"
- in "pass[1]", not in "pass[0]", so your comparison is offset by 1 character
- position.
-
- -- collins --
-
-